home *** CD-ROM | disk | FTP | other *** search
/ SGI Hot Mix 17 / Hot Mix 17.iso / HM17_SGI / research / examples / demo / demosrc / gettips.pro < prev    next >
Text File  |  1997-07-08  |  2KB  |  70 lines

  1. ;----------------------------------------------------------
  2. ;
  3. ;    PURPOSE  Read a file with a specified format (see below) and
  4. ;             returns the text structure.
  5. ;
  6. ;             File format :
  7. ;
  8. ;             N
  9. ;             id1 | text1
  10. ;             id2 | text2
  11. ;              ..    ..
  12. ;             idN | textN
  13. ;      
  14. ;             Where N is the number of lines( integer), idX and textX are
  15. ;             string, | is the seperator string. The seperator can
  16. ;             be customized by specifying the SEPERATOR keyword.
  17. ;
  18. function gettips, $
  19.     filename , $            ; IN: string containing the file name
  20.     SEPERATOR = seperator   ; IN: (opt) single character used as seperator.
  21.  
  22.     ;  Returns if the filename is not present.
  23.     ;
  24.     if (N_PARAMS() NE 1) then begin
  25.         PRINT, 'You must specify a file name'
  26.         PRINT, 'USAGE : textStructure = str2 (filename, SEPERATOR=characterString)'
  27.         RETURN, -1
  28.     endif
  29.  
  30.     ;  If the seperator is not specified, make the '|' as default.
  31.     ;
  32.     if (N_ELEMENTS(seperator) EQ 0) then begin
  33.         seperator = '|'
  34.     endif
  35.  
  36.     ;  Open the file.
  37.     ;
  38.     OPENR, lun, filename, /GET_LUN
  39.  
  40.     ;  Read the first item. It is the number of text lines in the file. 
  41.     ;
  42.     READF, lun,  nLines
  43.  
  44.     ;  Initialize the working arrays.
  45.     ;
  46.     str = ' '
  47.     parts = STRARR(nLines,2)
  48.  
  49.     ;  Read one line at a time. Seperate the line in two parts :
  50.     ;
  51.     ;  1) The string id
  52.     ;  2) The text body
  53.     ;
  54.     for i = 0, nLines-1 do begin
  55.         READF, lun,  str
  56.         parts[i,*] = STR_SEP(str, seperator)
  57.     endfor
  58.  
  59.     ;  Form the text structure.
  60.     ;
  61.     sText= { id : parts[*,0],  text : parts[*,1] } 
  62.  
  63.     CLOSE, lun
  64.     FREE_LUN, lun
  65.  
  66.     RETURN, sText
  67.  
  68. end
  69.  
  70.